package com.wisdytech.linkdcs.tool.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.linkdcs.tool.dao.ToolCheckItemDao;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.constants.DictKeyConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.system.dao.ISysUserDao;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.dao.TtEqToolDao;
import com.wisdytech.linkdcs.tool.model.EqToolConfig;
import com.wisdytech.linkdcs.tool.model.ToolCheckItem;
import com.wisdytech.linkdcs.tool.model.ToolConfig;
import com.wisdytech.linkdcs.tool.model.ToolMaintainItem;
import com.wisdytech.linkdcs.tool.model.TtEqTool;
import com.wisdytech.linkdcs.tool.service.ITtEqToolService;
import com.wisdytech.linkdcs.tool.vo.ToolCheckItemVO;
import com.wisdytech.linkdcs.tool.vo.ToolCheckVO;
import com.wisdytech.linkdcs.tool.vo.ToolMaintainItemVO;
import com.wisdytech.linkdcs.tool.vo.ToolMaintainVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wisdytech.linkdcs.tool.dao.ToolConfigDao;
import javax.annotation.Resource;
import java.util.*;
import com.wisdytech.linkdcs.tool.dao.ToolMaintainItemDao;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-05
 */
@Service
@Transactional
public class TtEqToolServiceImpl extends BaseService<TtEqToolDao, TtEqTool> implements ITtEqToolService {

    @Resource
    private ISysUserDao sysUserDao;

    @Resource
    private TtEqToolDao ttEqToolDao;
    @Resource
    private ToolConfigDao toolConfigDao;
    @Resource
    private ToolMaintainItemDao toolMaintainItemDao;
    @Resource
    private ToolCheckItemDao toolCheckItemDao;
//    @Override
//    public PageInfo<TtEqTool> listToolInfoByFilter(String toolCode,String toolName,String opCode,String toolModel,String status, Filter filter) {
//        Map<String, Object> params = new HashMap<>();
//        List<TtEqTool> list = this.beforeList(TtEqToolDao.class,filter).listToolInfoByFilter(params);
//        PageInfo<TtEqTool> page = new PageInfo<>(list);
//        return page;
//    }


    @Override
    public PageInfo<TtEqTool> listToolInfoByFilter(String toolCode, String toolName, String toolModel, String toolType, String opCode, String toolStatus, String checkDateStart, String checkDateEnd, String maintainFlag, String checkoutFlag, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(toolCode)) {
            params.put("toolCode",toolCode);
        }
        if (StringUtils.isNotBlank(toolName)) {
            params.put("toolName",toolName);
        }
        if (StringUtils.isNotBlank(toolModel)) {
            params.put("toolModel",toolModel);
        }
        if (StringUtils.isNotBlank(toolType)) {
            params.put("toolType",toolType);
        }
        if (StringUtils.isNotBlank(opCode)) {
            params.put("opCode",opCode);
        }
        if (StringUtils.isNotBlank(toolStatus)) {
            params.put("toolStatus",toolStatus);
        }
        if (StringUtils.isNotBlank(checkDateStart)) {
            params.put("checkDateStart",checkDateStart);
        }
        if (StringUtils.isNotBlank(checkDateEnd)) {
            params.put("checkDateEnd",checkDateEnd);
        }
        if (StringUtils.isNotBlank(maintainFlag)) {
            params.put("maintainFlag",maintainFlag);
        }
        if (StringUtils.isNotBlank(checkoutFlag)) {
            params.put("checkoutFlag",checkoutFlag);
        }
        List<TtEqTool> list = this.beforeList(TtEqToolDao.class,filter).listToolInfoByFilter(params);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean saveOrUpdateTool(TtEqTool ttEqTool, SysUser operator) {
        Boolean flag = false;
        if(operator == null)
            throw new ServiceException("操作人不能为空");
        if(ttEqTool == null)
            throw new ServiceException("参数错误");

        //校验文档编号是否重复
        EntityWrapper<TtEqTool> ew =new EntityWrapper<>();
        ew.eq("tool_code",ttEqTool.getToolCode());
        ew.eq("del_flag", CommonConstants.DEL_FLAG);
        //修改
        if(StringUtils.isNotBlank(ttEqTool.getPkId())) {
            ew.ne("pk_id",ttEqTool.getPkId());
            Integer count = ttEqToolDao.selectCount(ew);
            if(count>0) {
                throw new ServiceException("工装编码重复");
            }
            ttEqTool.setUpdateUser(operator.getUsername());
            ttEqTool.setUpdateDate(new Date());

            //TODO 更改周期在下个保养周期再计算
            TtEqTool tool = ttEqToolDao.selectById(ttEqTool.getPkId());
            // 保养状态由否改为是 则修改工装下次保养时间为工装信息中的保养开始时间
            if ("0".equals(tool.getMaintainFlag())&&"1".equals(ttEqTool.getMaintainFlag())) {
                ttEqTool.setNextMaintainDate(ttEqTool.getMaintainDate());
            }
            // 校验状态由否改为是 则修改工装下次校验时间为工装信息中的校验开始时间
            if ("0".equals(tool.getCheckoutFlag())&&"1".equals(ttEqTool.getCheckoutFlag())) {
                ttEqTool.setNextCheckoutDate(ttEqTool.getNextCheckoutDate());
            }

            Integer updateCount = ttEqToolDao.updateById(ttEqTool);
            if(updateCount > 0)
                flag = true;
        } else {
            Integer count = ttEqToolDao.selectCount(ew);
            if(count>0) {
                throw new ServiceException("工装编码重复");
            }
            ttEqTool.setPkId(BaseUtil.getUUID());
            ttEqTool.setCreateDate(new Date());
            ttEqTool.setCreateUser(operator.getUsername());
            ttEqTool.setDelFlag(CommonConstants.DEL_FLAG);

            //插入下次保养开始时间
            ttEqTool.setNextMaintainDate(ttEqTool.getMaintainDate());
            //插入下次校验开始时间
            ttEqTool.setNextCheckoutDate(ttEqTool.getCheckoutDate());
            Integer insertCount = ttEqToolDao.insert(ttEqTool);
            if(insertCount > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public Boolean deleteTool(String pkIds, SysUser operator) {
        if(operator == null)
            throw new ServiceException("操作人不能为空");
        String[] deletePkIds = pkIds.split(",");
        if(deletePkIds.length == 0) return true;
        List<String> idList = new ArrayList<String>();
        String[] id = pkIds.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        
        for(String pkId: idList) {
        	//判断是否配置
        	TtEqTool TtEqTool = ttEqToolDao.selectById(pkId);
        	EntityWrapper<ToolConfig> ew = new EntityWrapper<>();
        	ew.eq("TOOL_CODE", TtEqTool.getToolCode());
        	int count = toolConfigDao.selectCount(ew);
        	if(count>0) {
        		throw new ServiceException("工装已被配置");
        	}
        	EntityWrapper<ToolMaintainItem> ew1 = new EntityWrapper<>();
        	ew.eq("TOOL_CODE", TtEqTool.getToolCode());
        	//判断是否添加保养项
        	int num = toolMaintainItemDao.selectCount(ew1);
        	if(num>0) {
        		throw new ServiceException("工装已添加保养项");
        	}
        	//判断是否添加校验项
        	EntityWrapper<ToolCheckItem> ew2 = new EntityWrapper<>();
        	ew.eq("TOOL_CODE", TtEqTool.getToolCode());
        	//判断是否添加保养项
        	int num1 = toolCheckItemDao.selectCount(ew2);
        	if(num1>0) {
        		throw new ServiceException("工装已添加校验项");
        	}
        }
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        Integer deleteCount = ttEqToolDao.deleteBatchIds(pkIdCollection);
        return deleteCount > 0;
    }

    @Override
    public TtEqTool getToolInfoById(String pkId) {
        if(StringUtils.isBlank(pkId)) {
            return null;
        }
        return this.ttEqToolDao.getToolInfoById(pkId);
    }

    @Override
    public List<TtEqTool> getToolList(String search) {
        EntityWrapper<TtEqTool> ew = new EntityWrapper<>();
        ew.setSqlSelect("pk_id","tool_code","tool_name");
        ew.eq("del_flag",CommonConstants.DEL_FLAG);
        if(StringUtils.isNotBlank(search)) {
            ew.andNew().like("tool_code",search).or().like("tool_name",search);
        }
        return ttEqToolDao.selectList(ew);
    }

    @Override
    public PageInfo<TtEqTool> getToolMaintainList(String toolCode, String toolName, String toolModel, String toolType, String nextMaintainDateStart, String nextMaintainDateEnd,Filter filter) {
        Map<String, Object> params = new HashMap<>();
        params.put("toolTypeParentKey", DictKeyConstants.TOOL_TYPE_PARENT_KEY);
        if(StringUtils.isNotBlank(toolCode)) {
            params.put("toolCode",toolCode);
        }
        if(StringUtils.isNotBlank(toolName)) {
            params.put("toolName",toolName);
        }
        if(StringUtils.isNotBlank(toolModel)) {
            params.put("toolModel",toolModel);
        }
        if(StringUtils.isNotBlank(toolType)) {
            params.put("toolType",toolType);
        }
        if(StringUtils.isNotBlank(nextMaintainDateStart)) {
            params.put("nextMaintainDateStart",nextMaintainDateStart);
        }
        if(StringUtils.isNotBlank(nextMaintainDateEnd)) {
            params.put("nextMaintainDateEnd",nextMaintainDateEnd);
        }
        List<TtEqTool> list = this.beforeList(TtEqToolDao.class,filter).getToolMaintainList(params);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<TtEqTool> getToolCheckList(String toolCode, String toolName, String toolModel, String toolType, String opCode, String nextCheckDateStart, String nextCheckDateEnd, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        params.put("toolTypeParentKey", DictKeyConstants.TOOL_TYPE_PARENT_KEY);
        if(StringUtils.isNotBlank(toolCode)) {
            params.put("toolCode",toolCode);
        }
        if(StringUtils.isNotBlank(toolName)) {
            params.put("toolName",toolName);
        }
        if(StringUtils.isNotBlank(toolModel)) {
            params.put("toolModel",toolModel);
        }
        if(StringUtils.isNotBlank(toolType)) {
            params.put("toolType",toolType);
        }
        if(StringUtils.isNotBlank(opCode)) {
            params.put("opCode",opCode);
        }
        if(StringUtils.isNotBlank(nextCheckDateStart)) {
            params.put("nextCheckDateStart",nextCheckDateStart);
        }
        if(StringUtils.isNotBlank(nextCheckDateEnd)) {
            params.put("nextCheckDateEnd",nextCheckDateEnd);
        }
        List<TtEqTool> list = this.beforeList(TtEqToolDao.class,filter).getToolCheckList(params);
        return new PageInfo<>(list);
    }

    @Override
    public ToolMaintainVO getToolMaintainInfo(String toolCode) {
        if(StringUtils.isBlank(toolCode))
            return null;
        //查询工装基本信息
        TtEqTool toolParams = new TtEqTool();
        toolParams.setToolCode(toolCode);
        toolParams.setDelFlag(CommonConstants.DEL_FLAG);
        TtEqTool toolInfo = ttEqToolDao.selectOne(toolParams);
        if(toolInfo == null)
            throw new ServiceException("工装不存在");
        ToolMaintainVO vo = new ToolMaintainVO();
        vo.setToolCode(toolInfo.getToolCode());
        vo.setToolName(toolInfo.getToolName());

        //查询工装检验项信息
        List<ToolMaintainItemVO> itemList = this.ttEqToolDao.getToolMaintainItemList(toolCode);
        vo.setMaintainItemList(itemList);
        return vo;
    }

    @Override
    public ToolCheckVO getToolCheckInfo(String toolCode) {
        if(StringUtils.isBlank(toolCode))
            return null;
        //查询工装基本信息
        TtEqTool toolParams = new TtEqTool();
        toolParams.setToolCode(toolCode);
        toolParams.setDelFlag(CommonConstants.DEL_FLAG);
        TtEqTool toolInfo = ttEqToolDao.selectOne(toolParams);
        if(toolInfo == null)
            throw new ServiceException("工装不存在");
        ToolCheckVO vo = new ToolCheckVO();
        vo.setToolCode(toolInfo.getToolCode());
        vo.setToolName(toolInfo.getToolName());

        //查询工装检验项信息
        List<ToolCheckItemVO> itemList = this.ttEqToolDao.getToolCheckItemList(toolCode);
        vo.setCheckItemList(itemList);
        return vo;
    }

    @Override
    public PageInfo<SysUser> getUserPageList(String search,Filter filter) {
        EntityWrapper<SysUser> ew =new EntityWrapper<>();
        ew.setSqlSelect("pk_id","truename","username","work_no");
        if(StringUtils.isNotBlank(search)) {
            ew.like("work_no",search);
        }
        ew.eq("del_flag", CommonConstants.DEL_FLAG)
                .orderBy("CREATE_DATE", false);
        //List<SysUser> list = this.beforeList(ISysUserDao.class,filter).selectList(ew);
        List<SysUser> list = sysUserDao.selectList(ew);
        return new PageInfo<>(list);
    }
}
